#include <Arduino.h>
#include<LedControl.h>
#include <OneButton.h>
#define datapin 4
#define clkpin 5
#define cspin 6

#define xpin A1
#define ypin A2

OneButton button(10,true);

LedControl lc = LedControl(datapin,clkpin,cspin,1);
unsigned long lasttime;
byte matrix[8][8]={0};  //点阵数组
byte snakex[64]={4,5,6};  //贪吃蛇从蛇头到尾的x坐标
byte snakey[64]={4,4,4};  //贪吃蛇从蛇头到尾的Y坐标
byte snakelength=3;  //贪吃蛇初始长度
byte direction = 0 ; //贪吃蛇的方向，0向左，1向右，2向上，3向下
byte lastdir =0;
byte dotx;
byte doty;
bool life=1;  //表明贪吃蛇是否活着
bool shine;

void creatdot();

//蛇的惯性移动
void snakemove()
{
  if(millis()-lasttime>500)
  {
    //贪吃蛇从蛇尾开始，记录前一个的位置，除了蛇头
    for(int i=snakelength-1;i>0;i--)
    {
      snakex[i]=snakex[i-1];
      snakey[i]=snakey[i-1];
    }

    //蛇头要朝前进方向移动
    switch(direction)
    {
      case 0:
        if(snakex[0]>0)
          snakex[0]--;
        else
          snakex[0]=7;
        break;
      case 1:
        if(snakex[0]<7)
          snakex[0]++;
        else
          snakex[0]=0;
        break;
      case 2:
        if(snakey[0]>0)
          snakey[0]--;
        else
          snakey[0]=7;
        break;
      case 3:
        if(snakey[0]<7)
          snakey[0]++;
        else
          snakey[0]=0;
        break;

    }
    lastdir =direction;
    lasttime = millis();
  }
  if(snakex[0]== dotx && snakey[0]==doty)
  {
    snakelength++;
    snakex[snakelength-1]=snakex[snakelength-2];
    snakey[snakelength-1]=snakey[snakelength-2];
    creatdot();
  }
}
void update()
{
  //点阵全部清零
  for(int i=0;i<8;i++)
    for(int j=0;j<8;j++)
      matrix[i][j]=0;

  //更新蛇的位置
  for(int i=0;i<snakelength;i++)
  {
    matrix[snakey[i]][snakex[i]]=1;
  }

  //更新点的位置
  matrix[doty][dotx]=1;
}
void display()
{
  for(int i=0;i<8;i++)
    for(int j=0;j<8;j++)
      lc.setLed(0,i,j,matrix[i][j]);
}

void creatdot()
{
  randomSeed(analogRead(A0));
  dotx = random(0,8);
  doty = random(0,8);
  for(int i=0;i<snakelength;i++)
  {
    if(dotx == snakex[i] && doty == snakey[i])
    {
      creatdot();
      return ;
    }
  }
}

void lifecheck()
{
  for(int i=1;i<snakelength;i++)
    if(snakex[0] == snakex[i] && snakey[0] == snakey[i])
    {
        life=0;
        shine=1;
        lasttime = millis();
        break;
    }

}

void reborn()
{
  snakex[0]=4;
  snakex[1]=5;
  snakex[2]=6; //贪吃蛇从蛇头到尾的x坐标
  for(int i=3;i<64;i++)
    snakex[i]=0;
  
  snakey[0]=4;
  snakey[1]=4;
  snakey[2]=4;;  //贪吃蛇从蛇头到尾的Y坐标
  for(int i=3;i<64;i++)
    snakey[i]=0;
  snakelength=3;
  life=1;
  creatdot();
  lasttime = millis();
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(xpin,INPUT);
  pinMode(ypin,INPUT);
  pinMode(A0,INPUT);
  lc.shutdown(0,false);
  lc.setIntensity(0,1);
  lasttime =millis();
  creatdot();
  button.attachClick(reborn);

}

void keyscan()
{
   int vx=analogRead(xpin);
  int vy=analogRead(ypin);
 // Serial.println(String(vx)+"    " +String(vy));
  if(lastdir<2 && vy ==1023)
    direction = 3;
  else if(lastdir<2 && vy ==0)
    direction = 2;
  else if(lastdir>1 && vx==1023)
    direction = 1;
  else if(lastdir>1 && vx == 0)
    direction =0;
 // Serial.println(String(direction));
}

void gameover()
{
  // if(life==0)
  // {
  //   while(life==0);
  // }
  if(millis()-lasttime>=500)
  {
    if(shine)
    {
      update();
      display();
      Serial.println(shine);
      shine=!shine;

    }
    else
    {
      for(int i=0;i<8;i++)
        for(int j=0;j<8;j++)
          matrix[i][j]=0;
      display();
      Serial.println(shine);
      shine=!shine;

    }
    lasttime = millis();
    
  }
}


void loop() {
  // put your main code here, to run repeatedly:

  keyscan();
  button.tick();
  if(life)
  {
    snakemove();
    lifecheck();
  }
  if(!life)
    gameover();
  else
  {
    update();
    display();

  }
  
}